﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Collections.Generic;


using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Web.Project;
using PowerCreator.LMS.Course.Entity;
using PowerCreator.LMS.Web.Course;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Common.Utils;
using PowerCreator.LMS.DL.Project.Entity;
using PowerCreator.LMS.Web.DL.Project;


public partial class sysadmin_CourseManager_ManageCoursePinion : PowerCreator.LMS.Web.Base.BasePage
{

    protected PowerCreator.LMS.Web.Config.WebConfig config = new PowerCreator.LMS.Web.Config.WebConfig();
    PowerCreator.LMS.Web.DL.Project.TOCourseList objCourselist = new PowerCreator.LMS.Web.DL.Project.TOCourseList();
    PowerCreator.LMS.ViewQuery.ViewQuery ov = new PowerCreator.LMS.ViewQuery.ViewQuery();
    TOPinion opPinion = new TOPinion();
    private PowerCreator.LMS.Web.DL.User.LoginEntity login = new PowerCreator.LMS.Web.DL.User.LoginEntity();
    protected string courseID = "0";
    protected string courseListID = "0";
    private int type = 1;
    protected Course opCourse = new Course();
    private string url = "../login.aspx";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["type"] != null)
            {
                type = int.Parse(Request.QueryString["type"]);
                ViewState["type"] = type;
                if (type == 1)
                {
                    if (Request.QueryString["CourseListID"] != null)
                    {
                        courseListID = Request.QueryString["CourseListID"].ToString();
                        ViewState["CourseListID"] = courseListID;
                    }

                }
                else
                {
                    if (Request.QueryString["CourseID"] != null)
                    {
                        courseID = Request.QueryString["CourseID"].ToString();
                    }
                    ViewState["CourseID"] = courseID;

                }
            }

            InitPage();
            //BinderCoursersType();
            BinderScroeStr();
            string search = GetSearch();
            BinderPager(search);
            BinderRPT(search, 1);
           // BinderResourceClass();
        }
    }


    private bool CheckUser()
    {
        bool rt = false;
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            Script.RedirectNewUrl(url);
        }
        else
        {
            login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
            if (login == null)
            {
                Script.RedirectNewUrl(url);
            }
            else
            {
                if (login.VOAdvancedUser == null)
                {
                    Script.RedirectNewUrl(url);
                }
                else if (login.VOAdvancedUser.AUserGroup == 1 || login.VOAdvancedUser.AUserGroup == 2)
                {
                    rt = true;
                }
            }
        }

        return rt;

    }
    protected override void InitPage()
    {
        base.InitPage();
    }
    private void BinderScroeStr()
    {
        string sql = "";
        int total=0;
        int num=0;
        int per=0;
        DataTable dt = new DataTable();
        if (ViewState["type"].ToString() == "1")
        {
            VOTOCourseList vo = objCourselist.Load(int.Parse(ViewState["CourseListID"].ToString()));
            lbl_CourseName.Text = vo.CourseListName;
            sql = " CourseListID=" + ViewState["CourseListID"].ToString();
            num = opPinion.Count(sql);
            if (num > 0)
            {
                sql = "select sum(Score) as sum from TO_Pinion where CourseListID=" + ViewState["CourseListID"].ToString() + "";
                dt = ov.Query(sql);
                if (dt.Rows.Count == 1)
                {
                    total = int.Parse(dt.Rows[0]["sum"].ToString());
                    per = total / num;
                }
            }

        }
        else
        {
            VOTOCourse voCourse = opCourse.LoadVOTCourse(int.Parse(ViewState["CourseID"].ToString()));
            lbl_CourseName.Text = voCourse.CourseName;
            sql = " CourseID=" + ViewState["CourseID"].ToString();
            num = opPinion.Count(sql);
            if (num > 0)
            {
                sql = "select sum(Score) as sum from TO_Pinion where CourseID=" + ViewState["CourseID"].ToString() + "";
                dt = ov.Query(sql);
                if (dt.Rows.Count == 1)
                {
                    total = int.Parse(dt.Rows[0]["sum"].ToString());
                    per = total / num;
                }
            }
        }

        lbl_ScroeStr.Text = "参与评价人数：" + num.ToString() + ",总分：" + total.ToString() + ",平均分：" + per.ToString();


    }
    

    private string GetSearch()
    {
        string search = " 1=1";
        if (int.Parse(ViewState["type"].ToString()) == 1)
        {
            search = search + " and CourseListID=" + ViewState["CourseListID"].ToString() + "";
        }
        else
        {
            search = search + " and CourseID=" + ViewState["CourseID"].ToString() + "";

        }
        if (!string.IsNullOrEmpty(txt_SearchKey.Text.Trim()))
        {
            search = search + " and Title like '%" + txt_SearchKey.Text.Trim() + "'";
        }

        return search;

    }
    private void BinderPager(string search)
    {
        AspNetPager1.PageSize = LMSPageSize;
        AspNetPager1.RecordCount = opPinion.Count(search);
    }
    private void BinderRPT(string search,int pageIndex)
    {
        string orderList = " ID desc";
        DataTable dt = opPinion.Query(search, orderList, LMSPageSize, pageIndex);

        rptPinion.DataSource = dt;
        rptPinion.DataBind();
    }


    protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
        {
            LinkButton lnkbtn_look = (LinkButton)e.Item.FindControl("lnkbtn_look");
            LinkButton lbtn = (LinkButton)e.Item.FindControl("lnkbtn_del");
            if (CheckUser())
            {
                lnkbtn_look.Enabled = true;
                lbtn.Enabled = true;
                lbtn.Attributes.Add("onClick", "javascript:return confirm('确定删除此记录？');");
            }
            else
            {
                lnkbtn_look.Enabled = false;
                lbtn.Enabled = false;
            }
        }
    }

  

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderRPT(search, AspNetPager1.CurrentPageIndex);
    }
    protected void btn_Search_ServerClick(object sender, EventArgs e)
    {
        ViewState["SearchType"] = "1";//0简单搜索1高级搜索
        string search = GetSearch();
        BinderPager(search);
        BinderRPT(search, 1);
    }

    protected void rptResource_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName.ToLower() == "llook")
        {

            string url = "ShowPinion.aspx?ID=" + e.CommandArgument.ToString();
            Script.NewOpenURL(url);
            
        }
      
        else if (e.CommandName.ToLower() == "ldel")
        {
                if (opPinion.Del(int.Parse(e.CommandArgument.ToString())))
                {
                    BinderScroeStr();
                    string search = GetSearch();
                    BinderPager(search);
                    BinderRPT(search, AspNetPager1.CurrentPageIndex);
                }
                InitPage();
        }
       
    }
  
    protected void btn_SortSearch_Click(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderPager(search);
        BinderRPT(search, AspNetPager1.CurrentPageIndex);
    }
  
}